# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


class YouTube():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "authority": "www.youtube.com",
            "method": "POST",
            "path": "/comment_service_ajax?action_get_comments=1&pbj=1&ctoken=EkQSC2RvVlYxYTdYZ3lRwAEAyAEA4AEBogINKP___________wFAAMICGxoXaHR0cHM6Ly93d3cueW91dHViZS5jb20iABgG&continuation=EkQSC2RvVlYxYTdYZ3lRwAEAyAEA4AEBogINKP___________wFAAMICGxoXaHR0cHM6Ly93d3cueW91dHViZS5jb20iABgG&itct=CKcBEMm3AiITCP6Ci4SbteYCFVV3YAodFsMNvg%3D%3D",
            "scheme": "https",
            "accept": "*/*",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/x-www-form-urlencoded",
            "cookie": "YSC=v-V44xpZdOg; VISITOR_INFO1_LIVE=jptH2FUdtjE; GPS=1; PREF=f1=10000000&f4=4000000",
            "dnt": "1",
            "origin": "https://www.youtube.com",
            "referer": "https://www.youtube.com/watch?v=doVV1a7XgyQ&feature=youtu.be",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
            "x-client-data": "CJe2yQEIorbJAQjBtskBCKmdygEIy67KAQj3tMoBCIe2ygEYq6TKAQ==",
            "x-spf-previous": "https://www.youtube.com/watch?v=doVV1a7XgyQ&feature=youtu.be",
            "x-spf-referer": "https://www.youtube.com/watch?v=doVV1a7XgyQ&feature=youtu.be",
            "x-youtube-ad-signals": "dt=1576328974265&flash=0&frm&u_tz=480&u_his=11&u_java&u_h=864&u_w=1536&u_ah=824&u_aw=1536&u_cd=24&u_nplug=3&u_nmime=4&bc=31&bih=362&biw=1519&brdim=0%2C0%2C0%2C0%2C1536%2C0%2C1536%2C824%2C1536%2C362&vis=1&wgl=true&ca_type=image&bid=ANyPxKrbVnZmyZu52HpYF2REwHJLV8pblFkV3M9uWQ8uSg-QjBe6FxnquOjSlPCKGq65UOC4d4quJLXBl5IZduJAA5yf-Lh7iw",
            "x-youtube-client-name": "1",
            "x-youtube-client-version": "2.20191212.06.01",
            "x-youtube-device": "cbr=Chrome&cos=Windows&cbrver=79.0.3945.79&cosver=10.0",
            "x-youtube-page-cl": "285259775",
            "x-youtube-page-label": "youtube.ytfe.desktop_20191211_6_RC1",
            "x-youtube-time-zone": "Asia/Shanghai",
            "x-youtube-utc-offset": "480",
            "x-youtube-variants-checksum": "fded4b889854130286046e1e241a0140"
        }


    def get_contents(self):
        postData = {

        }

        url = "https://blz.bicoin.com.cn/settingFirmOffer/showAllLeaderList?pageSize=20&pageNum=1&sortType=5&sort=&type=future&searchName="
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        selector = etree.HTML(html.text)
        xpath = selector.xpath('//div[@class="col-xs-12 col-sm-7 listing-description"]')
        for i in xpath:
            aa = i.xpath('./div/div/p//text()')
            bb = i.xpath('./ul[@class="listing-features"]/li//text()')
            cc = i.xpath('./ul[@class="listing-features pull-left"]/li//text()')
            pass


    # 封装请求方法
    def get_request(self, method, url, headers, data):
        # cookies = self.get_cookie_dict("JSESSIONID=D79B2E69AADDABD4A2A0B5352C4A1987")
        try:
            if method == "POST":
                if headers == "":
                    if url.find("https") == 0:
                        res = requests.post(url, data=data, verify=False)
                    else:
                        res = requests.post(url, data=data)
                else:
                    if url.find("https") == 0:
                        res = requests.post(url, headers=headers, data=data, verify=False)
                    else:
                        res = requests.post(url, headers=headers, data=data)
            elif method == "GET":
                if headers == "":
                    if url.find("https") == 0:
                        res = requests.get(url, params=data, verify=False)
                    else:
                        res = requests.get(url, params=data)
                else:
                    if url.find("https") == 0:
                        res = requests.get(url, headers=headers, params=data, verify=False)
                    else:
                        res = requests.get(url, headers=headers, params=data)
            else:
                res = None
        except Exception as ex:
            print("-------------访问错误------------")
            print(ex)
            return HttpUtils.do_request(method, url, headers, data)
        else:
            if res.status_code != 200:
                print(f"-------------返回状态码:{res.status_code}------------")
                return HttpUtils.do_request(method, url, headers, data)
        return res


    # 将cookies字符串转换dict格式
    def get_cookie_dict(self, str):
        itemDict = {}
        items = str.split(';')
        for item in items:
            arr = item.split('=')
            key = arr[0].replace(' ', '')
            value = arr[1]
            itemDict[key] = value
        return itemDict


if __name__ == '__main__':
    youtube = YouTube()
    youtube.get_contents()